/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.invoice.header.controller;

import javax.validation.Valid;
import com.google.common.collect.Lists;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.aop.logging.annotation.ApiLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.invoice.header.service.dto.YxfpInvoiceHeaderDTO;
import com.jeeplus.invoice.header.service.mapstruct.YxfpInvoiceHeaderWrapper;
import com.jeeplus.invoice.header.service.YxfpInvoiceHeaderService;

/**
 * 抬头管理Controller
 * @author 张如利
 * @version 2022-01-20
 */

@Api(tags ="抬头管理")
@RestController
@RequestMapping(value = "/invoice/header/yxfpInvoiceHeader")
public class YxfpInvoiceHeaderController {

	@Autowired
	private YxfpInvoiceHeaderService yxfpInvoiceHeaderService;

	@Autowired
	private YxfpInvoiceHeaderWrapper yxfpInvoiceHeaderWrapper;

	/**
	 * 抬头列表数据
	 */
	@ApiLog("查询抬头列表数据")
	@ApiOperation(value = "查询抬头列表数据")
	@PreAuthorize("hasAuthority('invoice:header:yxfpInvoiceHeader:list')")
	@GetMapping("list")
	public ResponseEntity<IPage<YxfpInvoiceHeaderDTO>> list(YxfpInvoiceHeaderDTO yxfpInvoiceHeaderDTO, Page<YxfpInvoiceHeaderDTO> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (yxfpInvoiceHeaderDTO, YxfpInvoiceHeaderDTO.class);
		IPage<YxfpInvoiceHeaderDTO> result = yxfpInvoiceHeaderService.findPage (page, queryWrapper);
		return ResponseEntity.ok (result);
	}


	/**
	 * 根据Id获取抬头数据
	 */
	@ApiLog("根据Id获取抬头数据")
	@ApiOperation(value = "根据Id获取抬头数据")
	@PreAuthorize("hasAnyAuthority('invoice:header:yxfpInvoiceHeader:view','invoice:header:yxfpInvoiceHeader:add','invoice:header:yxfpInvoiceHeader:edit')")
	@GetMapping("queryById")
	public ResponseEntity<YxfpInvoiceHeaderDTO> queryById(String id) {
		return ResponseEntity.ok ( yxfpInvoiceHeaderService.findById ( id ) );
	}

	/**
	 * 保存抬头
	 */
	@ApiLog("保存抬头")
	@ApiOperation(value = "保存抬头")
	@PreAuthorize("hasAnyAuthority('invoice:header:yxfpInvoiceHeader:add','invoice:header:yxfpInvoiceHeader:edit')")
	@PostMapping("save")
	public  ResponseEntity <String> save(@Valid @RequestBody YxfpInvoiceHeaderDTO yxfpInvoiceHeaderDTO) {
		//新增或编辑表单保存
		yxfpInvoiceHeaderService.saveOrUpdate (yxfpInvoiceHeaderWrapper.toEntity (yxfpInvoiceHeaderDTO));
        return ResponseEntity.ok ( "保存抬头管理成功" );
	}


	/**
	 * 删除抬头
	 */
	@ApiLog("删除抬头")
	@ApiOperation(value = "删除抬头")
	@PreAuthorize("hasAuthority('invoice:header:yxfpInvoiceHeader:del')")
	@DeleteMapping("delete")
	public ResponseEntity <String> delete(String ids) {
		String idArray[] = ids.split(",");
        yxfpInvoiceHeaderService.removeByIds ( Lists.newArrayList ( idArray ) );
		return ResponseEntity.ok( "删除抬头管理成功" );
	}

}
